package finman.database;

/**
 * Encapsulate information about currency. Contain its name and description.
 * Valid currency names are: usd, eur, rub, uah.
 */
public class Valuta {
	private String name;
	private String description;

    /**
     * Creates currency specified by name. if name isn't valid,
     * constructor will throw IllegalArgumentException.
     */
	public Valuta(String name) throws IllegalArgumentException {
        if ( (!"usd".equals(name)) &&
             (!"eur".equals(name)) &&
             (!"rub".equals(name)) &&
             (!"uah".equals(name)) ) {
            throw new IllegalArgumentException("Unsupported currency name: " + name);
        } else {
            this.name = name;
            this.description = getDescriptionOfCurrency(name);
        }
	}

    private String getDescriptionOfCurrency(String currencyName) {
        if("usd".equals(currencyName)) {
            return "The United States dollar, " +
                    "also referred to as the American dollar, " +
                    "is the official currency of the United States of America.";
        }
        if("eur".equals(currencyName)) {
            return "The euro is the official currency of the eurozone";
        }
        if("rub".equals(currencyName)) {
            return "The ruble is the currency of the Russian Federation and the two " +
                    "partially recognized republics of Abkhazia and South Ossetia.";
        }
        if("uah".equals(currencyName)) {
            return "Ukrainian hryvnia, the national currency of Ukraine";
        }
        return null;
    }

    /**
     * Returns name of currency that this object represents.
     */
    public String getName() {
        return name;
    }

    /**
     * Returns description of currency that this object represents.
     */
    public String getDescription() {
        return description;
    }

    @Override
    public int hashCode() {
        int hashCode = 0;
        if (name != null) {
            hashCode += name.hashCode();
        }
        if (description != null) {
            hashCode += description.hashCode();
        }
        return hashCode * 17;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Valuta)) return false;

        Valuta valuta = (Valuta) obj;
        return valuta.getName().equals(name) &&
               valuta.getDescription().equals(description);
    }

    @Override
    public String toString() {
        return name;
    }
}